package com.collabscm.sample.jpa.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.collabscm.pf.dao.DynamicQueryStatement;
import com.collabscm.pf.util.ObjectUtils;
import com.collabscm.pf.util.RuntimeContext;
import com.collabscm.sample.demo.model.TestParty;

public class TestPartyDAO {
	@PersistenceContext(unitName="tryoutJPA")
	private EntityManager em;
	
	public TestPartyDAO() {
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("tryoutJPA");
		em = emf.createEntityManager();
	}
	
	public void insertParty(TestParty party) {
		em.getTransaction().begin();
		em.persist(party);
		em.getTransaction().commit();
	}
	
	public Integer getMaxPartyId() {
		return (Integer) em.createNamedQuery("TestParty.getMax").getSingleResult();
	}
	
	@SuppressWarnings("unchecked")
	public List<Object[]> getSimpleList() {
		return em.createNamedQuery("TestParty.getIdAndNameList").getResultList();
	}
	
	@SuppressWarnings("unchecked")
	public List getNativeList(String name) {
		Query q = em.createNativeQuery("TestParty.getNativeIdAndNameList");
		q.setParameter(1, name);
		return q.getResultList();
	}
	
	public void testDynaSQL(String name, Integer id) {
		StringBuffer dynaSQL = new StringBuffer("select ").append(TestParty.partyIdColumn).append(", party_name from test_t_party ");
		List<Object> parameters = new ArrayList<Object>(2);
		if(name != null) {
			dynaSQL = dynaSQL.append(" where party_name like ?");
			parameters.add("%" + name + "%");
		}
		if(id != null) {
			if(parameters.size() == 0) {
				dynaSQL.append(" where ");
			} else {
				dynaSQL.append(" and ");
			}
			dynaSQL.append(" party_id > ?");
			parameters.add(id);
		}
		Query q = em.createNativeQuery(dynaSQL.toString());
		em.createQuery("");
		for(int i=0; i<parameters.size(); i++) {
			q.setParameter(i + 1, parameters.get(i));
		}
		List result = q.getResultList();
		for(Object r: result) {
			System.out.println(ObjectUtils.toString(r));
		}
	}
	
	public static void main(String[] args) {
		TestPartyDAO dao = new TestPartyDAO();
		Integer maxId = dao.getMaxPartyId();
		TestParty party = new TestParty();
		Integer id = maxId + 1;
		party.setPartyId(id);
		party.setPartyName("Hello" + id);
		party.setCreator(1);
		party.setCreatedTime(RuntimeContext.getCurrentDate());
		party.setUpdator(1);
		party.setUpdatedTime(RuntimeContext.getCurrentDate());
		party.setEnableFlag("1");
		dao.insertParty(party);
		List<Object[]> list = dao.getSimpleList();
		for(Object[] t: list) {
			System.out.println(ObjectUtils.arrayToString(t));
		}
		@SuppressWarnings("unchecked")
		List<TestParty> l = dao.getNativeList("Hello");
		for(TestParty t: l) {
			System.out.println(ObjectUtils.toString(t));
		}
		dao.testDynaSQL("llo", 2);
	}
	
	private DynamicQueryStatement dQueryStmt;
	
	public void test(){
		dQueryStmt = new DynamicQueryStatement();
		dQueryStmt.addAlternativeFilter("partyName", "");
		String t = "select * from test_t_party where @t.partyName=:partyName and ";
	}
}
